package com.yycwxt.modules.admin.flowfee;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Flowfee;
import com.yycwxt.modules.admin.store.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Api
@Validated
@RestController
@RequestMapping("/flowfees")
public class FlowfeeController {

    private FlowfeeService flowfeeService;

    @Autowired
    public FlowfeeController(FlowfeeService flowfeeService) {
        this.flowfeeService = flowfeeService;
    }

    @GetMapping()
    @ApiOperation("查询全部物流费用表")
    public ReturnResult selectAll(){
        return ReturnResult.buildSuccessResult(flowfeeService.list());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询物流费用")
    public ReturnResult getById(@PathVariable Integer id){
        return ReturnResult.buildSuccessResult(flowfeeService.getById(id));
    }

    @PostMapping()
    @ApiOperation("flowfee表添加物流信息")
    public ReturnResult addFlowfee(@ApiParam(name = "flowfee对象",required = true, type = "flowfee") @RequestBody @Validated Flowfee flowfee){
        try {
            return ReturnResult.buildSuccessResult(flowfeeService.save(flowfee));
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("id值重复/其他属性为空");
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("flowfee表根据id删除物流信息")
    public ReturnResult deleteFlowfee(@ApiParam(name = "ID值",required = true, type = "int") @PathVariable Integer id){
        return ReturnResult.buildSuccessResult(flowfeeService.removeById(id));
    }

    @PutMapping()
    @ApiOperation("flowfee表根据id更改物流信息")
    public ReturnResult updateFlowfee(@ApiParam(name = "flowfee对象",required = true, type = "flowfee") @RequestBody @Validated Flowfee flowfee){
        return ReturnResult.buildSuccessResult(flowfeeService.updateById(flowfee));
    }

    @PostMapping("/upload")
    @ApiOperation("Excel导入数据")
    public ReturnResult excelToDatabase(@ApiParam(value = "是否删除全部历史记录", type = "boolean") @RequestParam boolean deleteHistory,
                                  @ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
        Result result = new Result();
        if(flowfeeService.importFileToDB(deleteHistory, file)){
            return ReturnResult.buildSuccessResult("添加成功",null);
        }else {
            return ReturnResult.buildFailureResult("添加失败",null);
        }
    }

    @GetMapping("/selectByInput")
    @ApiOperation("根据输入条件进行全文检索")
    public ReturnResult selectByInput(@ApiParam(name = "input检索内容", required = true, type = "String") String input){
        return ReturnResult.buildSuccessResult(flowfeeService.selectByInput(input));
    }
}
